﻿using DataSync.Entity;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace DataSync.Proc.CustParse
{
    class FileNameDateCustParse : ICustParse
    {
        private readonly string CNT_FileName = "FileName";
        private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public string Parse(string curValue, object[] rowValues, string custParams, CustParseExtData extParams)
        {
            if (extParams.DicData.ContainsKey(CNT_FileName))
            {
                string NewValue = curValue;
                try
                {
                    string fileName = extParams.DicData[CNT_FileName];
                    var CovParam = JsonConvert.DeserializeObject<FileNameDateConvertParams>(custParams);
                    Regex r = new Regex(CovParam.RegexPattern);
                    var dataStr = r.Match(fileName).Value;
                    NewValue = DateTime.ParseExact(dataStr + curValue, CovParam.DateFmt, System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd HH:mm:ss");
                }
                catch (Exception ex)
                {
                    log.Warn(string.Format("格式转换出错:{0}", curValue), ex);
                }
                return NewValue;
            }
            else
            {
                return curValue;
            }
        }

        public void Init(XElement xmlnode)
        {
            
        }
    }

    class FileNameDateConvertParams
    {
        public string RegexPattern { get; set; }
        public string DateFmt { get; set; }
    }
}
